package cn.tedu.egweibo1.controller;

import cn.tedu.egweibo1.mapper.CommentMapper;
import cn.tedu.egweibo1.pojo.dto.CommentDTO;
import cn.tedu.egweibo1.pojo.entity.Comment;
import cn.tedu.egweibo1.pojo.vo.CommentVO;
import cn.tedu.egweibo1.pojo.vo.UserVO;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/comment/")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("insert")
    public int insert(@RequestBody CommentDTO commentDTO, HttpSession session) {
        UserVO user = (UserVO) session.getAttribute("user");
       if (user == null) {
           return 2;
       }
       Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setCreated(new Date());
        comment.setUserId(user.getId());
        commentMapper.insert(comment);
        return 1;
    }

    @GetMapping("selectByWeiboId")
    public List<CommentVO> selectByWeiboId(Long id) {
        return commentMapper.selectByWeiboId(id);
    }
}
